package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysPost;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysPostService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysPostController
 * @描述 <br>岗位信息业务处理</br>
 * @日期 2019-03-31
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/post")
@Api(value = "岗位信息", description = "系统用户岗位信息业务处理")
public class SysPostController extends BaseController {
	private String prefix = "system/post";

	@Autowired
	private SysPostService postService;

	@RequiresPermissions("system:post:view")
	@GetMapping()
	public String operlog() {
		return prefix + "/post";
	}

	@ApiOperation(value = "岗位列表", notes = "岗位信息列表")
	@RequiresPermissions("system:post:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysPost post) {
		startPage();
		List<SysPost> list = postService.selectPostList(post);
		return getDataTable(list);
	}

	@Log(title = "岗位管理", businessType = BusinessType.EXPORT)
	@ApiOperation(value = "导出岗位", notes = "导出岗位信息列表数据到Excel")
	@RequiresPermissions("system:post:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysPost post) {
		List<SysPost> list = postService.selectPostList(post);
		ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
		return util.exportExcel(list, "岗位数据");
	}

	@RequiresPermissions("system:post:remove")
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除岗位", notes = "根据岗位id删除岗位信息")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			return toResult(postService.deletePostByIds(ids));
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 新增岗位
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存岗位
	 */
	@RequiresPermissions("system:post:add")
	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增岗位", notes = "新增保存岗位信息")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysPost post) {
		post.setCreateUser(ShiroUtils.getLoginName());
		return toResult(postService.insertPost(post));
	}

	/**
	 * 修改岗位
	 */
	@GetMapping("/edit/{postId}")
	public String edit(@PathVariable("postId") Long postId, ModelMap modelMap) {
		modelMap.put("post", postService.selectPostById(postId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存岗位
	 */
	@RequiresPermissions("system:post:edit")
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改岗位", notes = "修改保存岗位信息")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysPost post) {
		post.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(postService.updatePost(post));
	}

	/**
	 * 校验岗位名称
	 */
	@PostMapping("/checkPostNameUnique")
	@ApiOperation(value = "校验岗位名称", notes = "校验岗位名称唯一性")
	@ResponseBody
	public String checkPostNameUnique(SysPost post) {
		return postService.checkPostNameUnique(post);
	}

	/**
	 * 校验岗位编码
	 */
	@PostMapping("/checkPostCodeUnique")
	@ApiOperation(value = "校验岗位编码", notes = "校验岗位编码唯一性")
	@ResponseBody
	public String checkPostCodeUnique(SysPost post) {
		return postService.checkPostCodeUnique(post);
	}
}
